<?php 
namespace Api\Controller;
class LessonapiController extends CommonController{
	
	
	function _initialize() {
		parent::_initialize();
	}
	
	
	
	//根据课程id获取支付人数
	public function getnumberbylid($lid){
		$term  = A('Api/Term')->get_term_info();
		$w = array('tid'=>$term['id'],'lid'=>$lid);
		return M('YgclStudentNumber')->where($w)->getField('number');
	}
	
	//课程人数减1
	public function lesson_number_dec($tid,$lid){
		$term  = A('Api/Term')->get_term_by_id($tid);
		$xuenian_arr = explode(',',$term['studyyear']);
		foreach ($xuenian_arr as $key=>$xn){
			$xnid = R('Api/Termtime/get_xnid',array($xn));
			$w = array('xnid'=>$xnid,'lid'=>$lid);
			M('YgclStudentNumber')->where($w)->setDec('number');
		}
	}
	
	//课程人数加1
	public function lesson_number_inc($tid,$lid){
		$term  = A('Api/Term')->get_term_by_id($tid);
		$xuenian_arr = explode(',',$term['studyyear']);
		foreach ($xuenian_arr as $key=>$xn){
			$xnid = R('Api/Termtime/get_xnid',array($xn));
			$w = array('xnid'=>$xnid,'lid'=>$lid);
			M('YgclStudentNumber')->where($w)->setInc('number');
		}
	}
	
	//换班信息-更换人数
	public function changelsinfo($old_tid,$tid,$old_lid,$lid){
		$this->lesson_number_dec($old_tid,$old_lid);
		$this->lesson_number_inc($tid,$lid);
	}
	
	
	/**
	 * 获取课程类别
	 * @param unknown $data 
	 * @param unknown $classtype 类别数组
	 */
	public function getclasstype($data,$classtype){
		foreach ($data as $k=>$dt){
			$data[$k]['lessontype_title'] = $classtype[$dt['lessontype']];
		}
		return $data;
	}
	
	/**
	 * 获取学年的报名人数
	 * @param unknown $xuenian 学年(字符串)
	 * @param unknown $lid 课程id
	 */
	public function getnumber_by_xuenian($xuenian,$lid){
		//班级允许报名的总人数
		$number = M('YgclLesson')->where(array('id'=>$lid))->getField('number');
		$xuenian_arr = explode(',',$xuenian);
		$data = array('bool' => true,'xnid' => '');
		foreach ($xuenian_arr as $key=>$xn){
			$xnid = R('Api/Termtime/get_xnid',array($xn));
			$num = $this->get_number_by_xnid_lid($xnid,$lid);
			if($num >= $number){
				$data = array(
					'bool' => false,
					'xnid' => $xnid		
				);
				break;
			}
		}
		return $data;
	}
	
	//创建学年跟课程跟报名人数统计表数据
	public function create_student_number_data($xnid,$lid){
		$m = M('YgclStudentNumber');
		$d = array(
			'xnid'=>$xnid,
			'lid'=>$lid,
			'create_time'=> Date('Y-m-d h:i:s',time()),
			'update_time'=> Date('Y-m-d h:i:s',time()),
		);
		$id = $m->add($d);
		return $id;
	}
	
	/**
	 * 根据学年id跟课程id获取班级人数
	 * @param unknown $xnid
	 * @param unknown $lid
	 */
	public function get_number_by_xnid_lid($xnid,$lid){
		$students = $this->get_students_by_xnid($xnid,$lid);
		return count($students);
	}
	
	/**
	 * 根据学年id跟班级id获取班级人数
	 * @param unknown $xnid
	 * @param unknown $cid
	 */
	public function get_number_by_xnid_cid($xnid,$cid){
		$w= array('status'=>1,'xuenian'=>array('like','%X'.$xnid.'N%'),'cid'=>$cid);
		$students = M('LessonStudentRelation')->where($w)->select();
		return count($students);
	}
	
	/**
	 * 获取学年学生
	 * @param unknown $xnid
	 * @param unknown $lid
	 */
	public function get_students_by_xnid($xnid,$lid){
		
		//获取学生
		$w= array('status'=>1,'xuenian'=>array('like','%X'.$xnid.'N%'),'lid'=>$lid);
		$students = M('LessonStudentRelation')->where($w)->select();
		return $students;
	} 
	
	/**
	 * 获取当前学年的学生
	 * @param unknown $lid
	 */
	public function get_students_by_now_xnid($lid){
		$xuenian = R('Api/Termtime/get_term_time_now');
		return $this->get_students_by_xnid($xuenian['id'], $lid);
	}
	
	//体验人数验证
	public function check_tiyan($lid){
		$xuenian = R('Api/Termtime/get_term_time_now');
		$data = $this->getnumber_by_xuenian($xuenian, $lid);
		return $data;
	}
	
	/**
	 * 获取校区、课程、班级id
	 * @param unknown $lid
	 */
	public function _get_did_cid_lid($lid){
		$cid = M('YgclLesson')->where(array('id'=>$lid))->getField('cid');
		$did = M('YgclClass')->where(array('id'=>$cid))->getField('did');
		$data = array(
				'did'=>$did,
				'cid'=>$cid,
				'lid'=>$lid
		);
		return $data;
	}
	
	/**
	 * 根据lid获取校区课程班级
	 * @param unknown $lid
	 */
	public function get_xiaoqu_class_lesson_by_lid($lid){
		$data = $this->_get_did_cid_lid($lid);
		$xiaoqu = M('YgclDistrict')->where(array('id'=>$data['did']))->getField('title');
		$class = M('YgclClass')->where(array('id'=>$data['cid']))->getField('title');
		$lesson = M('YgclLesson')->where(array('id'=>$lid))->getField('lessoncode');
		return array('xiaoqu'=>$xiaoqu,'class'=>$class,'lesson'=>$lesson);
	}
}